package com.elegantmall.controller;

import com.elegantmall.dto.SalesReportDTO;
import com.elegantmall.dto.TextReportDTO;
import com.elegantmall.service.SalesReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Calendar;

@RestController
@RequestMapping("/api/reports")
public class SalesReportController {

    @Autowired
    private SalesReportServiceImpl salesReportService;

    /**
     * 生成总体销售报告
     */
    @GetMapping("/sales")
    public ResponseEntity<Map<String, Object>> generateSalesReport(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        if (startDate == null) {
            // 默认查询近30天的数据
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            startDate = calendar.getTime();
        }
        
        if (endDate == null) {
            endDate = new Date(); // 当前日期
        }
        
        SalesReportDTO report = salesReportService.generateSalesReport(startDate, endDate);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "销售报告生成成功");
        response.put("data", report);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 生成特定卖家的销售报告
     */
    @GetMapping("/sales/seller/{sellerName}")
    public ResponseEntity<Map<String, Object>> generateSellerSalesReport(
            @PathVariable String sellerName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        if (startDate == null) {
            // 默认查询近30天的数据
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            startDate = calendar.getTime();
        }
        
        if (endDate == null) {
            endDate = new Date(); // 当前日期
        }
        
        SalesReportDTO report = salesReportService.generateSellerSalesReport(sellerName, startDate, endDate);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "卖家销售报告生成成功");
        response.put("data", report);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 生成特定类别的销售报告
     */
    @GetMapping("/sales/category/{category}")
    public ResponseEntity<Map<String, Object>> generateCategorySalesReport(
            @PathVariable String category,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        if (startDate == null) {
            // 默认查询近30天的数据
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            startDate = calendar.getTime();
        }
        
        if (endDate == null) {
            endDate = new Date(); // 当前日期
        }
        
        SalesReportDTO report = salesReportService.generateCategorySalesReport(category, startDate, endDate);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "类别销售报告生成成功");
        response.put("data", report);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 生成文字版销售报告（包含销售趋势折线图）
     */
    @GetMapping("/text-report")
    public ResponseEntity<Map<String, Object>> generateTextSalesReport(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        if (startDate == null) {
            // 默认查询近30天的数据
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            startDate = calendar.getTime();
        }
        
        if (endDate == null) {
            endDate = new Date(); // 当前日期
        }
        
        TextReportDTO report = salesReportService.generateTextSalesReport(startDate, endDate);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "文字版销售报告生成成功");
        response.put("data", report);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 生成卖家文字版销售报告（包含销售趋势折线图）
     */
    @GetMapping("/text-report/seller/{sellerName}")
    public ResponseEntity<Map<String, Object>> generateTextSellerSalesReport(
            @PathVariable String sellerName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        if (startDate == null) {
            // 默认查询近30天的数据
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -30);
            startDate = calendar.getTime();
        }
        
        if (endDate == null) {
            endDate = new Date(); // 当前日期
        }
        
        TextReportDTO report = salesReportService.generateTextSellerSalesReport(sellerName, startDate, endDate);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", sellerName + "的文字版销售报告生成成功");
        response.put("data", report);
        
        return ResponseEntity.ok(response);
    }
} 